package question.solution841;

import question.SolutionTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Solution841 implements SolutionTemplate {

    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        boolean[] roomOpenState = new boolean[rooms.size()];
        Stack<Integer> uncheckedRoom = new Stack<>();
        uncheckedRoom.push(0);
        roomOpenState[0] = true;
        do{
            Integer top = uncheckedRoom.pop();
            List<Integer> keys = rooms.get(top);
            for(Integer key: keys){
                if(!roomOpenState[key]){
                    roomOpenState[key] = true;
                    uncheckedRoom.push(key);
                }
            }
        }while(!uncheckedRoom.empty());

        for(boolean open : roomOpenState){
            if(!open){
                return false;
            }
        }
        return true;
    }

    @Override
    public void test() {
//        List<Integer> room0 = List.of(1,3);
//        List<Integer> room1 = List.of(3,0,1);
//        List<Integer> room2 = List.of(2);
//        List<Integer> room3 = List.of(0);
//        List<List<Integer>> rooms = List.of(room0,room1,room2,room3);
//        boolean result = canVisitAllRooms(rooms);
//        System.out.println(result);
    }

}
